<template>
  <view class="history-section">
    <view class="history-header">
      <text class="section-title">喝奶记录</text>
      <view class="header-actions">
        <button v-if="showAddButton" class="action-btn add-btn" @click="onAddRecord">
          <text class="action-text">添加记录</text>
        </button>
        <button class="action-btn refresh-btn" @click="onRefresh" :disabled="isRefreshing">
          <text class="action-text">刷新</text>
        </button>
      </view>
    </view>
    <view class="records-list" style="flex: 1;">
      <view v-if="records.length === 0" class="empty-state">
        <text class="empty-text">{{ emptyText }}</text>
      </view>
      <view v-for="(record, index) in records" :key="record._id" class="record-card">
        <view class="record-content" @click="onEditRecord(record)">
          <view class="record-main">
            <view class="time-column">
              <text class="time">{{ record.time }}</text>
              <text v-if="index === 0" class="time-interval">{{ getTimeInterval(record) }}</text>
            </view>
            <view class="amount-container">
              <text class="amount">{{ record.amount }} ml</text>
            </view>
            <view class="feeding-type-container">
              <text class="feeding-type">{{ getFeedingTypeText(record.feeding_type) }}</text>
            </view>
          </view>
          <text v-if="record.notes" class="notes">{{ record.notes }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'MilkHistory',
  props: {
    // 传入的记录列表
    records: {
      type: Array,
      default: () => []
    },
    // 是否正在刷新
    isRefreshing: {
      type: Boolean,
      default: false
    },
    // 当前时间，用于计算时间间隔
    currentTime: {
      type: Date,
      default: () => new Date()
    },
    // 是否显示添加记录按钮
    showAddButton: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    // 根据是否显示添加按钮返回不同的空状态文本
    emptyText() {
      return this.showAddButton 
        ? '点击添加记录按钮添加第一条记录吧~' 
        : '今天还没有喝奶记录哦~';
    }
  },
  methods: {
    // 获取喂养方式文本
    getFeedingTypeText(feedingType) {
      const feedingTypes = {
        bottle_breast: '母乳',
        bottle_formula: '奶粉'
      }
      return feedingTypes[feedingType] || '瓶喂'
    },

    // 计算时间间隔
    getTimeInterval(record) {
      if (!record) return ''

      const recordDateTime = new Date(`${record.date}T${record.time}`)
      const now = this.currentTime
      const diffMs = now - recordDateTime

      if (diffMs < 0) return '刚刚'

      const diffMinutes = Math.floor(diffMs / (1000 * 60))
      const diffHours = Math.floor(diffMinutes / 60)
      const diffDays = Math.floor(diffHours / 24)

      if (diffMinutes < 1) return '刚刚'
      if (diffMinutes < 60) return `${diffMinutes}分钟前`
      if (diffHours < 24) return `${diffHours}小时前`
      return `${diffDays}天前`
    },

    // 触发添加记录事件
    onAddRecord() {
      this.$emit('add-record')
    },

    // 触发刷新事件
    onRefresh() {
      this.$emit('refresh')
    },

    // 触发编辑记录事件
    onEditRecord(record) {
      this.$emit('edit-record', record)
    }
  }
}
</script>

<style scoped>
.history-section {
  background-color: rgba(255, 255, 255, 0.05);
  border-radius: 30rpx;
  padding: 30rpx 30rpx 30rpx;
  margin-bottom: 0;
  box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.2);
  border: 1rpx solid rgba(255, 105, 180, 0.15);
  position: relative;
  z-index: 1;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden; /* 确保内部滚动正常工作 */
  min-height: 0; /* 允许flex子项收缩到小于内容高度 */
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
  flex-wrap: nowrap;
}

.section-title {
  font-size: clamp(30rpx, 5vw, 36rpx);
  font-weight: bold;
  color: #fff;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
  flex: 1;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 15rpx;
}

.records-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  /* 隐藏滚动条但保持可滚动功能 */
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE/Edge */
  min-height: 0; /* 允许flex子项收缩到小于内容高度 */
  max-height: 100%; /* 最大高度为父容器的100% */
}

/* 为Webkit浏览器隐藏滚动条 */
.records-list::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

.empty-state {
  text-align: center;
  padding: 80rpx 40rpx;
  color: rgba(255, 255, 255, 0.7);
  background: rgba(255, 105, 180, 0.1);
  border-radius: 24rpx;
  border: 1rpx dashed rgba(255, 105, 180, 0.3);
}

.empty-text {
  font-size: clamp(26rpx, 4vw, 30rpx);
  opacity: 0.9;
  color: rgba(255, 255, 255, 0.9);
  text-shadow: 0 1rpx 3rpx rgba(0, 0, 0, 0.3);
}

.record-card {
  background: linear-gradient(135deg, #ff8a9b, #ff6b9d);
  border-radius: 20rpx;
  padding: 20rpx 25rpx;
  margin-bottom: 16rpx;
  box-shadow: 0 6rpx 20rpx rgba(255, 105, 180, 0.25);
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
  border: 1rpx solid rgba(255, 255, 255, 0.1);
}

.record-card:last-child {
  margin-bottom: 0;
}

.record-card:hover {
  transform: translateY(-2rpx);
  box-shadow: 0 6rpx 18rpx rgba(255, 105, 180, 0.25);
}

.record-card:active {
  transform: scale(0.98);
  box-shadow: 0 4rpx 15rpx rgba(255, 105, 180, 0.2);
}

.record-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.record-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10rpx;
  margin-bottom: 0;
  flex-wrap: nowrap;
  min-height: 70rpx;
  position: relative;
}

.time-column {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  flex: 1;
  gap: 10rpx;
}

.time {
  font-size: clamp(38rpx, 5.8vw, 42rpx);
  font-weight: bold;
  color: #ffffff;
  text-align: left;
  flex-shrink: 0;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2), 0 0 8rpx rgba(255, 255, 255, 0.4);
  letter-spacing: 1rpx;
}

.time-interval {
  font-size: clamp(22rpx, 3.5vw, 24rpx);
  color: rgba(255, 255, 255, 0.95);
  background-color: rgba(255, 255, 255, 0.25);
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  text-align: center;
  white-space: nowrap;
  line-height: 1.2;
  margin-top: -4rpx;
  margin-bottom: 2rpx;
}

.amount-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.amount {
  font-size: clamp(38rpx, 5.8vw, 42rpx);
  font-weight: bold;
  color: white;
  text-align: center;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2), 0 0 8rpx rgba(255, 255, 255, 0.4);
  display: inline-block;
  letter-spacing: 1rpx;
}

.feeding-type-container {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.feeding-type {
  font-size: clamp(24rpx, 4vw, 28rpx);
  color: white;
  background-color: rgba(255, 255, 255, 0.2);
  padding: 8rpx 16rpx;
  border-radius: 16rpx;
  border: 1rpx solid rgba(255, 255, 255, 0.3);
  text-align: center;
  white-space: nowrap;
  display: inline-block;
}

.notes {
  font-size: clamp(26rpx, 4vw, 28rpx);
  color: rgba(255, 255, 255, 0.9);
  word-break: break-all;
  margin-top: 12rpx;
  line-height: 1.4;
  flex-shrink: 0;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 110rpx;
  height: 60rpx;
  padding: 0 20rpx;
  border-radius: 30rpx;
  background: none;
  border: 1rpx solid rgba(255, 255, 255, 0.15);
  transition: all 0.3s ease;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
  -webkit-tap-highlight-color: transparent;
}

button.action-btn {
  background-color: transparent;
}

button.action-btn::after {
  border: none;
  outline: none;
}

.action-btn:active {
  transform: scale(0.95);
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.2);
}

.action-text {
  font-size: 28rpx;
  font-weight: 500;
  white-space: nowrap;
  color: rgba(255, 255, 255, 0.9);
}

.add-btn {
  background: rgba(255, 107, 157, 0.15);
  border-color: rgba(255, 107, 157, 0.3);
}

.add-btn .action-text {
  color: rgba(255, 255, 255, 0.9);
}

.refresh-btn {
  background: rgba(255, 105, 180, 0.15);
  border-color: rgba(255, 105, 180, 0.3);
}

.refresh-btn .action-text {
  color: rgba(255, 255, 255, 0.9);
}

.refresh-btn:active {
  background: rgba(255, 105, 180, 0.25);
  transform: scale(0.95);
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.2);
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.refresh-btn .action-text.rotating {
  animation: rotate 1s linear infinite;
}
</style> 